using System;
using System.Collections.Generic;
using System.Text;
using Entity;
using ECommerce.Library;
using ECommerce.Library.DataStructure;

namespace AutoGear.BLL
{
    public class UserProfile
    {
        private int _userProfileId;
        public int UserProfileId
        {
            get { return _userProfileId; }
            set { _userProfileId = value; }
        }

        private string _firstName;
        public string FirstName
        {
            get { return _firstName; }
            set { _firstName = value; }
        }

        private string _lastName;
        public string LastName
        {
            get { return _lastName; }
            set { _lastName = value; }
        }

        private DateTime _dateOfBirth;
        public DateTime DateOfBirth
        {
            get { return _dateOfBirth; }
            set { _dateOfBirth = value; }
        }

        private string _emailAddress;
        public string EmailAddress
        {
            get { return _emailAddress; }
            set { _emailAddress = value; }
        }

        private Gender _userGender;
        public Gender UserGender
        {
            get { return _userGender; }
            set { _userGender = value; }
        }

        private string _userTelephoneNumber;
        public string UserTelephoneNumber
        {
            get { return _userTelephoneNumber; }
            set { _userTelephoneNumber = value; }
        }

        private string _userFaxNumber;
        public string UserFaxNumber
        {
            get { return _userFaxNumber; }
            set { _userFaxNumber = value; }
        }

        private string _companyName;
        public string CompanyName
        {
            get { return _companyName; }
            set { _companyName = value; }
        }

        private string _companyTelephoneNumber;
        public string CompanyTelephoneNumber
        {
            get { return _companyTelephoneNumber; }
            set { _companyTelephoneNumber = value; }
        }

        private string _companyFaxNumber;
        public string CompanyFaxNumber
        {
            get { return _companyFaxNumber; }
            set { _companyFaxNumber = value; }
        }

        private Address _billingAddress;
        public Address BillingAddress
        {
            get { return _billingAddress; }
            set { _billingAddress = value; }
        }
        private Address _shippingAddress;
        public Address ShippingAddress
        {
            get { return _shippingAddress; }
            set { _shippingAddress = value; }
        }


        private bool _isActive;
        public bool IsActive
        {
            get { return _isActive; }
            set { _isActive = value; }
        }
        private bool _isDeleted;
        public bool IsDeleted
        {
            get { return _isDeleted; }
            set { _isDeleted = value; }
        }

        private DateTime _createDate;
        public DateTime CreateDate
        {
            get { return _createDate; }
            set { _createDate = value; }
        }

        private int _userId;
        public int UserId
        {
            get { return _userId; }
            set { _userId = value; }
        }

        private string _adminComment;
        public string AdminComment
        {
            get { return _adminComment; }
            set { _adminComment = value; }
        }
        public UserProfile()
        {

        }

        public UserProfile(int userProfileId, string firstName, string lastName, DateTime dateOfBirth, string emailAddress,
        Gender userGender, string userTelephoneNumber, string userFaxNumber, string companyName,
        string companyTelephoneNumber, string companyFaxNumber, Address billingAddress, Address shippingAddress,
            bool isActive, bool isDeleted, DateTime createDate, int userId, string adminComment)
        {
            this._userProfileId = userProfileId;
            this._firstName = firstName;
            this._lastName = lastName;
            this._dateOfBirth = dateOfBirth;
            this._emailAddress = emailAddress;
            this._userGender = userGender;
            this._userTelephoneNumber = userTelephoneNumber;
            this._userFaxNumber = userFaxNumber;
            this._companyName = companyName;
            this._companyTelephoneNumber = companyTelephoneNumber;
            this._companyFaxNumber = companyFaxNumber;
            this._billingAddress = billingAddress;
            this._shippingAddress = shippingAddress;
            this._isActive = isActive;
            this._isDeleted = isDeleted;
            this._createDate = createDate;
            this._userId = userId;
            this._adminComment = adminComment;
        }

        public UserProfile(TblUserProfile tblUserProfile)
        {
            this._userProfileId = tblUserProfile.UserProfileId;
            this._firstName = tblUserProfile.FirstName;
            this._lastName = tblUserProfile.LastName;
            this._dateOfBirth = tblUserProfile.DateOfBirth;
            this._emailAddress = tblUserProfile.EmailAddress;
            this._userGender = tblUserProfile.UserGender.ToLower() == "male" ? Gender.Male : Gender.Female;
            this._userTelephoneNumber = tblUserProfile.UserTelephoneNumber;
            this._userFaxNumber = tblUserProfile.UserFaxNumber;
            this._companyName = tblUserProfile.CompanyName;
            this._companyTelephoneNumber = tblUserProfile.CompanyTelephoneNumber;
            this._companyFaxNumber = tblUserProfile.CompanyFaxNumber;
            this._billingAddress = new Address(tblUserProfile.BillingFirstName, tblUserProfile.BillingLastName, tblUserProfile.BillingAddressLine1, tblUserProfile.BillingAddressLine2, tblUserProfile.BillingCity, tblUserProfile.BillingAddressState, tblUserProfile.BillingAddressCountry, tblUserProfile.BillingZipCode,tblUserProfile.BillingTelephoneNumber, string.Empty, tblUserProfile.BillingEmailAddress);
            this._shippingAddress = new Address(tblUserProfile.ShippingFirstName, tblUserProfile.ShippingLastName, tblUserProfile.ShippingAddressLine1, tblUserProfile.ShippingAddressLine2, tblUserProfile.ShippingCity, tblUserProfile.ShippingAddressState, tblUserProfile.ShippingAddressCountry, tblUserProfile.ShippingZipCode, tblUserProfile.ShippingTelephoneNumber, string.Empty, tblUserProfile.ShippingEmailAddress);
            this._isActive = tblUserProfile.IsActive;
            this._isDeleted = tblUserProfile.IsDeleted;
            this._createDate = tblUserProfile.CreateDate;
            this._userId = tblUserProfile.UserId;
            this._adminComment = tblUserProfile.AdminComment;
        }

        public static UserProfile Create(string firstName, string lastName, DateTime dateOfBirth, string emailAddress,
        Gender userGender, string userTelephoneNumber, string userFaxNumber, string companyName,
        string companyTelephoneNumber, string companyFaxNumber, Address billingAddress, Address shippingAddress,
            bool isActive, int userId, string adminComment)
        {
            string userGenderTxt = userGender == Gender.Male ? "male" : "female";
            bool isDeleted = false;

            firstName = Security.BindStringInput(firstName, SecurityLevel.Two);
            lastName = Security.BindStringInput(lastName, SecurityLevel.Two);
            emailAddress = Security.BindStringInput(emailAddress, SecurityLevel.One);
            companyName = Security.BindStringInput(companyName, SecurityLevel.One);

            int newUserProfileId = TblUserProfile.CreateNewTblUserProfile(firstName, lastName, dateOfBirth, emailAddress,
                                                                        userGenderTxt, userTelephoneNumber, userFaxNumber, companyName, companyTelephoneNumber, companyFaxNumber,
                                                                        billingAddress.FirstName, billingAddress.LastName, billingAddress.AddressLine1, billingAddress.AddressLine2, billingAddress.City,
                                                                        billingAddress.AddressState, billingAddress.AddressCountry, billingAddress.ZipCode, billingAddress.TelephoneNumber, billingAddress.EmailAddress, shippingAddress.FirstName, shippingAddress.LastName,
                                                                        shippingAddress.AddressLine1, shippingAddress.AddressLine2, shippingAddress.City, shippingAddress.AddressState, shippingAddress.AddressCountry,
                                                                        shippingAddress.ZipCode, shippingAddress.TelephoneNumber,shippingAddress.EmailAddress, isActive, isDeleted, DateTime.Now, userId, adminComment);

            UserProfile newUserProfile = UserProfile.GetUserProfileById(newUserProfileId);
            if (newUserProfile != null)
            {
                return newUserProfile;
            }
            else
            {
                return null;
            }
        }

        public static UserProfile GetUserProfileById(int userProfileId)
        {
            TblUserProfile tblUserProfile = TblUserProfile.GetTblUserProfileByUserProfileId(userProfileId);
            if (tblUserProfile != null)
            {
                UserProfile userProfile = new UserProfile(tblUserProfile);
                return userProfile;
            }
            else
            {
                return null;
            }
        }

        public void Update()
        {
            TblUserProfile.UpdateTblUserProfile(
                this._userProfileId,
                this._firstName,
                this._lastName,
                this._dateOfBirth,
                this._emailAddress,
                this._userGender.ToString(),
                this._userTelephoneNumber,
                this._userFaxNumber,
                this._companyName,
                this._companyTelephoneNumber,
                this._companyFaxNumber,
                this._billingAddress.FirstName, 
                this._billingAddress.LastName, 
                this._billingAddress.AddressLine1, 
                this._billingAddress.AddressLine2, 
                this._billingAddress.City,                                                                        
                this._billingAddress.AddressState, 
                this._billingAddress.AddressCountry, this._billingAddress.ZipCode,
                this._billingAddress.TelephoneNumber, this._billingAddress.EmailAddress, 
                this._shippingAddress.FirstName, this._shippingAddress.LastName,
                this._shippingAddress.AddressLine1, this._shippingAddress.AddressLine2, 
                this._shippingAddress.City, this._shippingAddress.AddressState, 
                this._shippingAddress.AddressCountry,
                this._shippingAddress.ZipCode,
                this._shippingAddress.TelephoneNumber, this._shippingAddress.EmailAddress, 
                this._isActive,
                this._isDeleted,
                this._createDate,
                this._userId,
                this._adminComment

                );
        }

        internal static UserProfile LoadByUserId(int userId)
        {
            ECommerce.DAL.CustomCollection<TblUserProfile> profiles = TblUserProfile.GetTblUserProfileByUserId(userId);
            if (profiles != null && profiles.Count>0)
            {
                UserProfile userProfile = new UserProfile(profiles[0]);
                return userProfile;
            }
            else
            {
                return null;
            }
        }
    }
}
